home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 201 / 201.xpi / components / migrationService.js < prev    next >
Text File  |  2010-01-11  |  7KB  |  214 lines

  1. /* You may find the license in the LICENSE file */
  2.  
  3. function include(uri) {
  4.     Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
  5.         .getService(Components.interfaces.mozIJSSubScriptLoader)
  6.         .loadSubScript(uri);
  7. }
  8. include('chrome://dta/content/common/xpcom.jsm');
  9.  
  10. var Preferences = {};
  11.  
  12. var MigrationService = {
  13.     _init: function MM_init() {
  14.         // observer registration
  15.         Cc['@mozilla.org/observer-service;1']
  16.                 .getService(Ci.nsIObserverService)
  17.                 .addObserver(this, "final-ui-startup", true);
  18.     },
  19.     
  20.     _migrate: function MM_migrate() {
  21.         let DTA = {};
  22.         Components.utils.import('resource://dta/version.jsm', DTA);        
  23.         Components.utils.import('resource://dta/preferences.jsm', Preferences);
  24.         include("chrome://dta/content/common/overlayFunctions.js");
  25.         
  26.         try {
  27.             debug("current " + DTA.VERSION);
  28.  
  29.             let lastVersion = Preferences.getExt('version', '0');
  30.             if (0 == DTA.compareVersion(DTA.BASE_VERSION, lastVersion)) {
  31.                 return;
  32.             }
  33.             debug("MigrationManager: migration started");
  34.             if (DTA.compareVersion(lastVersion, "1.0a1") < 0) {
  35.                 this._execute(['Prefs', 'DropDowns', 'Filters', 'Remove']);
  36.             }
  37.             if (DTA.compareVersion(lastVersion, "1.0.1") < 0) {
  38.                 this._execute(['ResetMaxConnections']);
  39.             }            
  40.         var params = Components.classes["@mozilla.org/embedcomp/dialogparam;1"]
  41.                 .createInstance(Components.interfaces.nsIDialogParamBlock);
  42.         params.SetNumberStrings(1);
  43.         params.SetString(0, DTA.BASE_VERSION);
  44.         let mediator = {};
  45.         Components.utils.import('resource://dta/mediator.jsm', mediator);
  46.         mediator.showNotice(null, params);        
  47.         }
  48.         catch(ex) {
  49.             debug("MigrationManager:", ex);
  50.             try {
  51.                 Preferences.resetExt("version");
  52.             }
  53.             catch (ex) {
  54.                 // XXX
  55.             }
  56.         }
  57.     },
  58.     _execute: function MM_execute(types) {
  59.         for each (let e in types) {
  60.             try {
  61.                 this['_migrate' + e]();
  62.             }
  63.             catch (ex) {
  64.                 debug('MigrationManager: failed to migrate ' + e, ex);
  65.             }
  66.         }
  67.     },
  68.     
  69.     // pre-1.0: convert prefs
  70.     _migratePrefs: function MM_migratePrefs() {
  71.         debug("migrating prefs");
  72.         const toMigrate = [
  73.             ['context.infophrases', 'infophrases', true],
  74.             ['context.closedta', 'closedta', false],
  75.             ['context.menu', 'ctxmenu', ''],
  76.             ['context.compact', 'ctxcompact', false],
  77.             ['tool.menu', 'toolsmenu', ''],
  78.             ['tool.compact', 'toolscompact', false],
  79.             ['context.history', 'history',5],
  80.             ['context.downloadWin', 'downloadWin', true],
  81.             ['context.ntask', 'ntask', 4],
  82.             ['context.maxchunks', 'maxchunks', 5],
  83.             ['context.reduce', 'showonlyfilenames', true],
  84.             ['context.saveTemp', 'saveTemp', true],
  85.             ['context.tempLocation', 'tempLocation', ''],
  86.             ['context.seltab', 'seltab', 0],
  87.             ['context.timeout', 'timeout', 300],
  88.             ['directory.visibledump', 'logging', false],
  89.             ['context.removeaborted', 'removeaborted', false],
  90.             ['context.removecanceled', 'removecanceled', false],
  91.             ['context.removecompleted', 'removecompleted', true],
  92.             ['numistance', 'counter', 0]
  93.         ];
  94.         for each (let [oldName, newName, defaultValue] in toMigrate) {
  95.             try {
  96.                 let nv = Preferences.getExt(newName, defaultValue);
  97.                 let ov = Preferences.getExt(oldName, nv);
  98.                 if (ov != nv) {    
  99.                     Preferences.setExt(newName, ov);
  100.                 }
  101.                 Preferences.reset(oldName);                
  102.             }
  103.             catch (ex) {
  104.                 debug('MM: failed ' + newName + ", ", ex);
  105.             }
  106.         }
  107.     },
  108.     
  109.     // 1.0.1: #613 Multiple "slow-down" reports
  110.     _migrateResetMaxConnections: function() {
  111.         debug("resetting connection prefs");
  112.         for each (let e in ['network.http.max-connections', 'network.http.max-connections-per-server', 'network.http.max-persistent-connections-per-server']) {
  113.             Preferences.reset(e);
  114.         }
  115.     },
  116.     
  117.     // pre 1.0: migrate Filters
  118.     _migrateFilters: function MM_migrateFilters() {
  119.         debug("migrating filters");
  120.         const defFilters = [
  121.             "/\./", "/\\./", '/(\\.*)/',
  122.             "/\\/[^\\/\\?]+\\.(z(ip|\\d{2})|r(ar|\\d{2})|jar|bz2|gz|tar|rpm)$/", "/\\/[^\\/\\?]+\\.(z(ip|[0-9]{2})|r(ar|[0-9]{2})|jar|bz2|gz|tar|rpm)$/", "/(\\.(z(ip|[0-9]{2})|r(ar|[0-9]{2})|jar|bz2|gz|tar|rpm))$/",
  123.             "/\\/[^\\/\\?]+\\.(mp(eg?|[g4])|rm|avi|mov|divx|asf|qt|wmv|ram|m1v|m2v|rv|vob|asx)$/", "/(\\.(mpeg|rm|mpe|avi|mpg|mp4|mov|divx|asf|qt|wmv|ram|m1v|m2v|rv|vob|asx))$/",
  124.             "/\\/[^\\/\\?]+\\.(jpe?g|jpe|gif|png|tiff?|bmp|ico)$/",
  125.             "/\\/[^\\/\\?]+\\.(jpe?g|jpe)$/", '/\\.jp(?:e|e?g|2)$/',
  126.             "/\\/[^\\/\\?]+\\.gif$/",
  127.             "/\\/[^\\/\\?]+\\.png$/"
  128.         ];
  129.         const LINK_FILTER = Ci.dtaIFilter.LINK_FILTER;
  130.         const IMAGE_FILTER = Ci.dtaIFilter.IMAGE_FILTER;
  131.         const prefs = Cc['@mozilla.org/preferences-service;1']
  132.             .getService(Components.interfaces.nsIPrefService)
  133.             .getBranch("extensions.dta.context.")
  134.             .QueryInterface(Components.interfaces.nsIPrefBranch2);
  135.         var c = {value: 0};
  136.         var children = prefs.getChildList('', c);
  137.         for (let i = 0; i < c.value; ++i) {
  138.             if (!children[i].match(/filter\d+\.caption/)) {
  139.                 continue;
  140.             }
  141.             var name = 'context.' + children[i].slice(0, -8);
  142.             try {
  143.                 var reg = Preferences.getExt(name + '.filter', '');
  144.                 if (-1 != defFilters.indexOf(reg) || !reg.length) {
  145.                     continue;
  146.                 }
  147.                 var label = Preferences.getExt(name + '.caption', 'imported');
  148.                 var active = Preferences.getExt(name + '.checked', false);
  149.                 var type = 0;
  150.                 if (Preferences.getExt(name + '.isImageFilter', false)) {
  151.                     type |= IMAGE_FILTER;
  152.                 }
  153.                 if (Preferences.getExt(name + '.isLinkFilter', false)) {
  154.                     type |= LINK_FILTER;
  155.                 }
  156.                 DTA_FilterManager.create(label, reg, active, type, true);
  157.             }
  158.             catch (ex) {
  159.                 debug("failed to migrate filter", ex);
  160.             }
  161.         }
  162.     },
  163.     
  164.     // pre 1.0: dropdown history
  165.     _migrateDropDowns: function MM_migrateDropdowns() {
  166.         debug("migrating dropdowns");
  167.         for each (let e in ['renaming', 'filter', 'directory']) {
  168.             try {
  169.                 Preferences.resetExt(e);
  170.             }
  171.             catch (ex) {
  172.                 /*no-op*/
  173.             }
  174.             try {
  175.                 let cv = Preferences.getExt('dropdown.' + e + '-history', null);
  176.                 if (cv == null) {
  177.                     return;
  178.                 }
  179.                 cv = cv.split('|@|');
  180.                 Preferences.setExt(e, cv.toSource());
  181.             }
  182.             catch (ex) {
  183.                 debug("failed to migrate dropdown " + e, ex);
  184.             }
  185.         }
  186.     },
  187.     
  188.     // all: remove all prefs
  189.     _migrateRemove: function MM_migrateRemove() {
  190.         for each (let e in ['context.', 'tool.', 'dropdown.', 'windows.', 'rename.']) {
  191.             Preferences.resetBranchExt(e);
  192.         }
  193.     },
  194.     
  195.     // nsIObserver
  196.     observe: function MM_observe(subject, topic, prefName) {
  197.         if (topic == "final-ui-startup") {
  198.             this._migrate();
  199.         }
  200.     }
  201. };
  202. implementComponent(
  203.     MigrationService,
  204.     Components.ID("{F66539C8-2590-4e69-B189-F9F8595A7670}"),
  205.     "@downthemall.net/migration-service;1",
  206.     "DownThemAll! Migration Service",
  207.     [Ci.nsIObserver]
  208. );
  209. MigrationService._init();
  210.  
  211. // entrypoint
  212. function NSGetModule(compMgr, fileSpec) {
  213.     return new ServiceModule(MigrationService, true);
  214. }